#import "scene_library.h"

@implementation scene_library

- (id)initSceneLibrary
{
	if ((self = [super init]))
	{
		[[CCDirector sharedDirector] setProjection:CCDirectorProjection2D];

		/*set up handle
		*/
		g_SceneLibrary = self;

		/*music
		*/
		MusicLoad();
		MusicPlayLibrary();
		[self schedule:@selector(tickMusicLibrary:) interval:BGM_LIBRARY_TIME + BGM_TIME_SPLITE];

		[self SetState:SCENE_LIBRARY_STATE_MAIN];
	}
	return self;
}

- (void)dealloc
{
	ClearLibraryHandle();
	[super dealloc];
}

- (void)tickSceneChangeToMenu:(ccTime)Time
{
	scene_dummy *_SceneDummy;

	[self unschedule:@selector(tickSceneChangeToGame:)];

	_SceneDummy = [[scene_dummy alloc] initSceneDummy:SCENE_CHANGE_TO_MENU];
	[[CCDirector sharedDirector] replaceScene:_SceneDummy];
	[_SceneDummy release];
}

- (void)tickMusicLibrary:(ccTime)Time
{
	MusicPlayLibrary();
}

- (void)tickMusicCredits:(ccTime)Time
{
	MusicPlayLibraryCredits();
}

- (void)SetState:(int)State
{
	[self FreeLayer];
	switch (State)
	{
	case SCENE_LIBRARY_STATE_MAIN:
		g_LayerLibraryMain = [[layer_library_main alloc] initLibraryMain];
		[self addChild:g_LayerLibraryMain];
		[g_LayerLibraryMain release];
		g_LayerLibraryMain.positionInPixels = CGPointZero;
		break;
	case SCENE_LIBRARY_STATE_EXPERIMENT:
		g_LayerLibraryExperiment = [[layer_library_experiment alloc] initLayerLibraryExperiment];
		[self addChild:g_LayerLibraryExperiment];
		[g_LayerLibraryExperiment release];
		g_LayerLibraryExperiment.positionInPixels = CGPointZero;
		break;
	case SCENE_LIBRARY_STATE_TREASURE:
		g_LayerLibraryTreasure = [[layer_library_treasure alloc] initLayerLibraryTreasure];
		[self addChild:g_LayerLibraryTreasure];
		[g_LayerLibraryTreasure release];
		g_LayerLibraryTreasure.positionInPixels = CGPointZero;
		break;
	case SCENE_LIBRARY_STATE_CREDITS:
		g_LayerLibraryCredits = [[layer_library_credits alloc] initLibraryCredits];
		[self addChild:g_LayerLibraryCredits];
		[g_LayerLibraryCredits release];
		g_LayerLibraryCredits.positionInPixels = CGPointZero;

		/*music
		*/
		MusicPlayLibraryCredits();
		[self schedule:@selector(tickMusicCredits:) interval:BGM_STAFF_TIME + BGM_TIME_SPLITE];
		[self unschedule:@selector(tickMusicLibrary:)];
		break;
	default:;
	}
}

- (void)FreeLayer
{
	if (g_LayerLibraryMain != nil)
	{
		[self removeChild:g_LayerLibraryMain cleanup:TRUE];
		g_LayerLibraryMain = nil;
	}
	if (g_LayerLibraryExperiment != nil)
	{
		[self removeChild:g_LayerLibraryExperiment cleanup:TRUE];
		g_LayerLibraryExperiment = nil;
	}
	if (g_LayerLibraryTreasure != nil)
	{
		[self removeChild:g_LayerLibraryTreasure cleanup:TRUE];
		g_LayerLibraryTreasure = nil;
	}
	if (g_LayerLibraryCredits != nil)
	{
		[self removeChild:g_LayerLibraryCredits cleanup:TRUE];
		g_LayerLibraryCredits = nil;

		/*music
		*/
		MusicPlayLibrary();
		[self schedule:@selector(tickMusicLibrary:) interval:BGM_LIBRARY_TIME + BGM_TIME_SPLITE];
		[self unschedule:@selector(tickMusicCredits:)];
	}
}

- (void)SceneChangeToMenu
{
	sprite_dummy *_Loading;

	/*music
	*/
	MusicStop();
	[self unschedule:@selector(tickMusicLibrary:)];
	[self unschedule:@selector(tickMusicCredits:)];

	/*free layers
	*/
	[self FreeLayer];

	/*loading image
	*/
	_Loading = [[sprite_dummy alloc] initSpriteDummy:true];
	[self addChild:_Loading];
	[_Loading release];
	_Loading.positionInPixels = g_ScrrenCenter;

	/*tick
	*/
	[self schedule:@selector(tickSceneChangeToMenu:) interval:1.0f];
}

@end